package com.yzy.domain.strategy.service.rule.tree.impl;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;


import com.yzy.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import com.yzy.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.yzy.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.yzy.types.common.Constants;
import com.yzy.types.common.Constants.RuleKey;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yangzhenyu05 <yangzhenyu05@kuaishou.com>
 * Created on 2024-01-27
 * @description 次数锁节点
 */
@Slf4j
@Component(RuleKey.RULE_LOCK_KEY)
public class RuleLockLogicTreeNode implements ILogicTreeNode {
    private final Long userRaffleCount = 10L;

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue) {
        log.info("规则过滤-次数锁 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
        long raffleCount = NumberUtils.LONG_ZERO;
        try {
            raffleCount = Long.parseLong(ruleValue);
        } catch (NumberFormatException exception) {
            throw new RuntimeException("规则过滤-次数锁异常 ruleValue: " + ruleValue + " 配置不正确");
        }
        if (userRaffleCount >= raffleCount) {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                    .build();
        }
        // 用户抽奖次数小于规则限定值，规则拦截
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
